<script>
    var ManageColumn = [
        {
            width: 60,
            title: '编号',
            scopedSlots: { customRender: 'index' },
        },
        {
            width: 80,
            title: '用户名',
            dataIndex: 'username',
        },
        {
            width: 80,
            title: '姓名',
            dataIndex: 'name',
        },
        {
            width: 100,
            title: '权限',
            dataIndex: 'role',
            scopedSlots: { customRender: 'role' },
        },
        {
            width: 100,
            title: '邮箱',
            dataIndex: 'email',
        },
        {
            width: 200,
            title: '操作时间',
            dataIndex: 'created_at',
            scopedSlots: { customRender: 'time' },
        },
        {
            width: 100,
            title: '密码',
            dataIndex: 'password',
            scopedSlots: {
                customRender: 'password'
            }
        },
        {
            width: 60,
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
        },
        {
            width: 60,
            title: '操作',
            dataIndex: 'operations',
            scopedSlots: {
                customRender: 'operations'
            }
        },
    ];
    var ManageOperate = {
        data: {
            ManageColumn,
            IsEdit:false,
            title:'添加菜单',
            RoleSelect:[],
            RoleObject:{},
            visible: false,             //添加修改菜单侧栏可见性控制
            ManageList:[],                //菜单列表
            SetManageRequest:{            //菜单信息，用于提交请求
                id : 0,
                name:'',
                username:'',
                password:'',
                email:'',
                is_staff:1,
                role_group:[],
            },
            DefaultSetManageRequest:{
                id : 0,
                name:'',
                username:'',
                password:'',
                email:'',
                is_staff:1,
                role_group:[],
            },
        },
        methods: {
            //页面初始化调用
            initManage(){
                this.getManageList()
                this.getRoleList()
                this.loadAsideDefaultOpenKeys()
            },

            //显示编辑侧栏
            //isEdit = 1 时为修改
            showManageOperate(isEdit) {
                if (isEdit > 0) {
                    //修改
                    this.title = '修改工作人员'
                    this.IsEdit = true
                } else {
                    this.SetManageRequest = JSON.parse(JSON.stringify(this.DefaultSetManageRequest))
                    this.title = '添加工作人员'
                    this.IsEdit = false
                }
                this.visible = true;
            },

            //隐藏编辑侧栏
            hideManageOperate() {
                this.visible = false;
            },

            //获取角色信息
            getRoleList(){
                axios.post('/role-set/get-list').then(res => {
                    if (res.data.ok) {
                        this.RoleSelect = res.data.list
                        let _this = this
                        res.data.list.forEach(function (item) {
                            _this.RoleObject[item.id] = item.title
                        })
                        console.log(this.RoleObject)
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //获取工作人员信息
            getManageList(){
                axios.post('/role-set/get-manage-list',).then(res => {
                    if (res.data.ok) {
                        this.ManageList = res.data.list
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //提交确认
            ManageConfirm() {
                let verify = this.verifyRequestParam()
                if (!verify) {
                    return
                }

                let param = JSON.parse(JSON.stringify(this.SetManageRequest))
                param.role_group = param.role_group.join(',')
                axios.post('/role-set/manage-save', param).then(res => {
                    if (res.data.ok) {
                        this.initManage();
                        success(res.data.msg)
                        this.SetManageRequest = JSON.parse(JSON.stringify(this.DefaultSetManageRequest))
                        this.hideManageOperate()
                    } else {
                        error('操作失败: ' + res.data.msg)
                    }
                })

            },

            //工作人员启用禁用
            statusUpdate(item,status){
                console.log(item)
                console.log(status)
                axios.post('/role-set/update-manage-status', { id: item.id,status:status}).then(res => {
                    if (res.data.ok) {
                        item.status = status
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //删除
            deleteConfirm(item,index) {
                let _this = this
                this.$confirm({
                    title: '系统提示',
                    content:  '确认删除该工作人员',
                    onOk() {
                        _this.delete(item,index)
                    }
                })
            },

            delete(item,index){
                axios.post('/role-set/delete-manage', { id: item.id}).then(res => {
                    if (res.data.ok) {
                        this.ManageList.splice(index,1)
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //修改
            editManage(item) {
                this.SetManageRequest = item
                console.log(item)
                this.showManageOperate(1)
            },

            usernameInput(){
                // this.SetManageRequest.username = this.SetManageRequest.username.replace(/([^\u0000-\u00FF])/g, '')
                // this.SetManageRequest.username = this.SetManageRequest.username.replace(/\s+/g,'')
            },

            //参数验证
            verifyRequestParam(){
                try {
                    if (this.SetManageRequest.username == '') {
                        throw '请输入登陆账户'
                    }

                    let usernamePattern = /^[a-zA-Z0-9_-]{4,16}$/
                    if (!usernamePattern.test(this.SetManageRequest.username)) {
                        this.SetManageRequest.username = ''
                        throw '请输入4到16位登陆账户'
                    }

                    if (this.SetManageRequest.password == '') {
                        throw '请输入账户密码'
                    }

                    if (this.SetManageRequest.name == '') {
                        throw '请输入用户名称'
                    }

                    return true

                } catch (e) {
                    error(e);
                    return false
                }
            }
        },
    };
</script>